/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jmooc.dao.jpa;

import com.jmooc.dao.QuestionDao;
import com.jmooc.dao.ReadCourseDao;
import com.jmooc.entity.Course;
import com.jmooc.entity.Question;
import com.jmooc.entity.ReadCourse;
import com.jmooc.entity.User;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;

/**
 *
 * @author Group
 */
@Stateless
public class JpaReadCourseDao implements ReadCourseDao {
    
    @PersistenceContext
    private EntityManager em;

    @Override
    public void create(ReadCourse readCourse) {
        em.persist(readCourse);
    }

    @Override
    public List<ReadCourse> getAllByUser(User user) {
        return em.createQuery("SELECT rc FROM ReadCourse rc WHERE rc.user = :u").setParameter("u", user).getResultList();
    }

    @Override
    public ReadCourse getByUserAndCourse(User user, Course course) {
        return (ReadCourse) em.createQuery("SELECT rc FROM ReadCourse rc WHERE rc.user = :u AND rc.course = :c")
                .setParameter("u", user).setParameter("c", course).getSingleResult();
    }
}